// pages/User/user.js
const _app = getApp()
Page({
  data: {
    userInfo: null, // 用于存储用户信息
    isAuthorized: false, // 用于标记用户是否已授权登录
    userInfo: {},
    hasUserInfo: false,
    userInfo: {},
        uindex: null,
        list: [{
            id: 'timecard',
            name: '类别1',
            open: false,
            pages: [{
                    path: "normal",
                    title: "功能1"
                },
                {
                    path: "history",
                    title: "功能2"
                }
            ]
        }, {
            id: 'overwork',
            name: '类别2',
            open: false,
            pages: [{
                    path: "create",
                    title: "功能1"
                },
                {
                    path: "list",
                    title: "功能2"
                }
            ]
        }, {
            id: 'sample',
            name: '类别3',
            open: false,
            pages: [{
                path: "index",
                title: "功能1"
            }]
        }]
  },
  onShow:function(){
    console.log(_app.globalData.appsecret);
    console.log(_app.globalData);
  },
  credit: function () {
    wx.request({
      url: 'http://127.0.0.1:3000/credit',
      data: { token: app.globalData.token },
      success: res => {
        console.log(res.data)
      }
    })
  },
  getUserInfo: function (e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    }
  },
  sendUserInfo: function () {
    var token = app.globalData.token
    wx.getUserInfo({
      success: res => {
        wx.request({
          url: 'http://127.0.0.1:3000/userinfo?token=' + token,
          method: 'post',
          data: {
            rawData: res.rawData,
            signature: res.signature,
            encryptedData: res.encryptedData,
            iv: res.iv
          }
        })
      }
    })
  },
  onLoad: function (options) {
    // 检查用户是否已经授权
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          // 用户已授权，获取用户信息
          this.getUserInfo();
        }
      },
    });
  },

  // 获取用户信息
  getUserInfo: function () {
    wx.getUserInfo({
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          isAuthorized: true,
        });
      },
    });
  },

    //事件处理函数
    kindToggle: function (e) {
        var id = e.currentTarget.id,
            list = this.data.list;
        for (var i = 0, len = list.length; i < len; ++i) {
            if (list[i].id == id) {
                list[i].open = !list[i].open
            } else {
                list[i].open = false
            }
        }
        this.setData({
            list: list
        });
    },
    onLoad: function () {
        // var that = this
        // //调用应用实例的方法获取全局数据
        // app.getUserInfo(function (userInfo) {
        //     //更新数据
        //     that.setData({
        //         userInfo: userInfo
        //     })
        // })
    }
})

